﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Dln.Common;
using Dln.Common.Helper;
using Dln.Model;
using System;
using System.Drawing;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using System.Threading;
using System.Web.Script.Serialization;
using Bright.Library.BO.Pub;
using Dln.Handler;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System.Text;

public partial class lltest : System.Web.UI.Page
{

    protected string getSignDigest31 = "http://192.168.2.85:9999/gateway/common/getSignDigest";
    protected string synRecipeInfo33 = "http://192.168.2.85:9999/gateway/recipe/synRecipeInfo";
    protected string synSignDataSearch35 = "http://192.168.2.85:9999/gateway/recipe/synSignDataSearch";
    protected string synDoctor32 = "http://192.168.2.85:9999/gateway/doctor/synDoctor";
    protected string synDoctorSearch34 = "http://192.168.2.85:9999//gateway/doctor/synDoctorSearch";
    protected string verifyData36 = "http://192.168.2.85:9999/gateway/recipe/verifyData";

    protected void Page_Load(object sender, EventArgs e)
    {
        string aa = "EH20190326144020594866826";
        if (aa.Substring(0, 2).ToUpper() == "EH")
        {
            var asdasdasd = "";
        }
        else
        {
            var aaaaaa = "";
        }
        Dln.Common.ServiceReference1.Service1SoapClient client2 = new Dln.Common.ServiceReference1.Service1SoapClient();
        object result = client2.GetCardID("刘雷","321322199112270218","19805233977");

    }

    public string GetAuthOpenId()
    {
        string AppId = CommonHelper.GetAppSettings("AppID");
        string AppSecret = CommonHelper.GetAppSettings("AppSecret");
        string strOpenId = "";
        string name = "";
        string code = DNTRequest.GetString("CODE");

        var cookies = HttpContext.Current.Request.Cookies["openidcookie"];
        if (cookies != null && !string.IsNullOrEmpty(cookies.Value))
        {
            strOpenId = cookies.Value;
        }
        else
        {
            if (!String.IsNullOrEmpty(code))
            {
                string json =
                    RequestHelper.SendGet(
                        String.Format(
                            "https://api.weixin.qq.com/sns/oauth2/access_token?appid={0}&secret={1}&code={2}&grant_type=authorization_code",
                            AppId, AppSecret, code));
                if (!String.IsNullOrEmpty(json))
                {
                    try
                    {
                        OAuthConfig oAuthConfig = JSONHelper.FromJson<OAuthConfig>(json);
                        if (oAuthConfig != null)
                        {
                            HttpCookie cookie = new HttpCookie("openidcookie");
                            cookie.Value = oAuthConfig.openid;
                            cookie.Expires = DateTime.Now.AddDays(2);
                            HttpContext.Current.Response.Cookies.Add(cookie);
                            strOpenId = oAuthConfig.openid;
                        }

                        else
                        {
                            strOpenId = "";
                        }
                    }
                    catch (Exception ex)
                    {
                        strOpenId = "";
                    }
                }
            }
            else
            {
                var url =
                    string.Format(
                        "https://open.weixin.qq.com/connect/oauth2/authorize?appid={0}&redirect_uri={1}%2Flltest.aspx&response_type=code&scope=snsapi_base&state=test#wechat_redirect",
                        AppId, CommonHelper.GetAppSettings("mainurl").Replace(@"://", "%3A%2F%2F"));
                Response.Redirect(url);

            }
        }
        return strOpenId;
    }

    public void ResponseToPage(string message)
    {
        Response.Redirect(String.Format("/page/Common/message.aspx?m={0}", Server.UrlEncode(message)));
    }


    /// <summary>
    /// 同步医师信息服务3.2
    /// </summary>
    /// <param name="data32"></param>
    /// <returns></returns>
    public string SynDoctor(string data32)
    {
        var reuslt32 = RequestHelper.SendRestPost(synDoctor32, data32);
        var jsonObj32 = (JObject)JsonConvert.DeserializeObject(reuslt32);
        if (jsonObj32 != null && jsonObj32["status"].ToString() == "0" && jsonObj32["data"] != null)
        {
            return jsonObj32["data"].ToString();
        }
        return "";
    }

    /// <summary>
    /// 同步医师信息服务3.1
    /// </summary>
    /// <param name="openid"></param>
    /// <returns></returns>
    public string GynDoctorSearch(string openid)
    {
        string data34 = "{" +
                        "\"head\": {" +
                        "\"clientId\": \"2018011614312929\"" +
                        "}," +
                        "\"body\": {" +
                        "\"openId\": \"" + openid + "\"" +
                        "}}";
        var reuslt = RequestHelper.SendRestPost(synDoctorSearch34, data34);
        var jsonObj34 = (JObject)JsonConvert.DeserializeObject(reuslt);
        if (jsonObj34 != null && jsonObj34["status"].ToString() == "0" && jsonObj34["data"] != null)
        {
            return jsonObj34["data"].ToString();
        }
        ////保存医生签章 stamp
        return "";
    }

    /// <summary>
    /// 3.1 签名指纹计算接口
    /// </summary>
    /// <param name="data31"></param>
    /// <returns></returns>
    public string GetSignDigest(string data31)
    {
        try
        {

            var reuslt31 = RequestHelper.SendPost(getSignDigest31, data31);
            var jsonObj = (JObject)JsonConvert.DeserializeObject(reuslt31);
            if (jsonObj != null && jsonObj["status"].ToString() == "0")
            {
                var datajson = (JObject)JsonConvert.DeserializeObject(jsonObj["data"] + "");
                if (datajson != null)
                {
                    return datajson + "";
                }
            }
        }
        catch
        {
            return "";
        }
        return "";
    }

    /// <summary>
    /// 3.3 签名数据同步接口
    /// </summary>
    /// <param name="data33"></param>
    /// <returns></returns>
    public string SynRecipeInfo(string data33)
    {
        try
        {
            var reuslt33 = RequestHelper.SendRestPost(synRecipeInfo33, data33);
            var jsonObj = (JObject)JsonConvert.DeserializeObject(reuslt33);
            if (jsonObj != null && jsonObj["status"].ToString() == "0" && jsonObj["data"] != null)
            {
                return jsonObj["data"] + "";
            }
        }
        catch
        {
            return "";
        }
        return "";
    }

    /// <summary>
    /// 3.5 签名数据签名结果查询
    /// </summary>
    /// <returns></returns>
    public string SynSignDataSearch(string data35)
    {
        try
        {
            var json = "{" +
                            "\"head\": {" +
                            "\"clientId\": \"2018011614312929\"" +
                            "}," +
                            "\"body\": {" +
                            "\"uniqueId\": \"" + data35 + "\"" +
                            "}}";


            var reuslt = RequestHelper.SendRestPost(synSignDataSearch35, json);
            var jsonObj35 = (JObject)JsonConvert.DeserializeObject(reuslt);
            if (jsonObj35 != null && jsonObj35["status"].ToString() == "0" && jsonObj35["data"] != null)
            {
                return jsonObj35["data"] + "";
            }
        }
        catch
        {
            return "";
        }
        return "";
    }
}
